package org.example.demo.controller.userController;

import io.swagger.annotations.ApiOperation;
import org.example.demo.common.api.CommonResult;
import org.example.demo.dto.ProductShowParam;
import org.example.demo.service.userService.ShopService;
import org.example.demo.service.userService.UserCenterService;
import org.example.mbg.mapper.ProductMapper;
import org.example.mbg.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping(value = "/user/center")
public class UserCenterController {
    @Autowired
    private UserCenterService userCenterService;

    @Autowired
    private ProductMapper productMapper;

    @ApiOperation("获得用户余额")
    @RequestMapping(value = "/balance", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BigDecimal> getBalance(Long user_id) {
        return CommonResult.success(userCenterService.getBalance(user_id));
    }

    @ApiOperation("获得用户收藏")
    @RequestMapping(value = "/collect", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<ProductShowParam>> getCollect(Long user_id) {
        return CommonResult.success(userCenterService.getFavs(user_id).stream().map(e -> {
            Long productId = e.getShopId();
            ProductShowParam productShowParam = new ProductShowParam();
            Product product = productMapper.selectByPrimaryKey(productId);
            if (product != null) {
                productShowParam.setId(product.getId());
                productShowParam.setPage(product.getPage());
                productShowParam.setTitle(product.getName());
                productShowParam.setImg(product.getPic());
                productShowParam.setSales(product.getSale());
                productShowParam.setPrice(product.getPrice());
                productShowParam.setDiscount(product.getDiscount());
                productShowParam.setServices(product.getServices());
                productShowParam.setCfav(product.getCfav());
                return productShowParam;
            }
            return null;
        }).collect(Collectors.toList()));
    }
}
